<?php
/**
 * @api {get} http://aicomm.bndvip.com/Api/My/houseList/ 获取房子列表
 * @apiName houseList
 * @apiGroup My
 *
 * @apiParam {String} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/My/userDetail/ 个人信息详情
 * @apiName userDetail
 * @apiGroup My
 *
 * @apiParam {String} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/My/userModify/ 更改个人信息
 * @apiName userModify
 * @apiGroup My
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {String} birthday 用户生日 2018-09-01(选填).
 * @apiParam {Number} sex 性别 1男 2女(选填).
 * @apiParam {String} nickname 用户昵称(选填).
 * @apiParam {String} mobile 手机号码(选填).
 * @apiParam {String} email 邮箱(选填).
 * @apiParam {String} userModify 签名(选填).
 * @apiParam {Number} code 存在手机号码一定存在code(选填).
 * @apiParam {String} signature 签名(选填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/My/handimg/ 修改头像
 * @apiName handimg
 * @apiGroup My
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {String}  image 图片(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/My/feedback/ 留言
 * @apiName feedback
 * @apiGroup My
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {String} image 图片(选填).
 * @apiParam {String} con 内容(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/My/privacy/ 隐私设置
 * @apiName privacy
 * @apiGroup My
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {String} privacyid 三种设置 0为开启1为关闭 通过0-0-1相连内容(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
namespace app\api\controller;
use think\Page;
use think\Db;
use think\Model;
use \think\Log;
use app\common\controller\Api;
class My extends Base
{
    //获取房子列表
    public function houseList(){
        $userid = I('request.user_id');

        if ($userid > 0 || !empty($userid)){
            //搜索为户主的数量
            $huzuwhere['uid'] = array('eq',$userid);
            $huzuwhere['status'] = array('eq',1);
            $list = M('auths')->where($huzuwhere)->select();
            //搜索非户主的房子
            if ($list){
                $return = codemsg(3);
                $return['data'] = $list;
                Log::api('【My/houseList】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }else{
                $return = codemsg(-7030);
                Log::api('【My/houseList】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }else{
            $return = codemsg(-9);
            Log::api('【My/houseList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }
    //个人信息详情
    public function userDetail(){
        $userid = I('request.user_id');
        if ($userid > 0 || !empty($userid)){
            //查询个人信息
            $where['user_id'] = array('eq',$userid);
            $where['is_lock'] = array('eq',0);
            $data = M('users')->field('sex,email,mobile,nickname,head_pic,signature')->where($where)->find();
            if ($data['head_pic'] == ''){
                $data['head_pic'] = '/upload/pic/default.jpg';
            }
            if ($data['nickname'] == ''){
                $data['nickname'] = $data['mobile'];
            }
            if ($data['sex'] == 1){
                $data['sex'] = '男';
            }else{
                $data['sex'] = '女';
            }
            if ($data){
                $return = codemsg(3);
                $return['data'] = $data;
                Log::api('【My/userDetail】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }else{
                $return = codemsg(-4003);
                Log::api('【My/userDetail】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }else{
            $return = codemsg(-9);
            Log::api('【My/userDetail】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }
    //更改个人信息
    public function userModify(){
        $userid = I('request.user_id');
        $birthday = I('request.birthday');
        $sex = I('request.sex');
        $nickname = I('request.nickname');
        $mobile = I('request.mobile');
        $signature = I('request.signature');
        $email = I('request.email');
        if (empty($userid) && $userid <=0 && empty($birthday) && empty($sex) && empty($nickname) && empty($mobile)){
            $return = codemsg(-9);
            Log::api('【My/userModify】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //搜索该手机号的账号
        $userwhere['user_id'] = array('eq',$userid);
        $userwhere['is_lock'] = array('eq',0);
        $user = M('users')->where($userwhere)->find();
        if (empty($user)){
            $return = json_encode(codemsg(-4003));
            Log::login('【My/userModify】-[return]--'.json_encode($return));
            echo $return;die;
        }
        //存在手机号码
        if (!empty($mobile)){
            $code = I('request.code');
            $checkphone = regcheck('phone',$mobile);
            if ($checkphone){
                $return = $checkphone;
                Log::login('【My/userModify】-[return]--'.json_encode($return));
                echo $return;die;
            }
            //手机验证码
            if (empty($code)) {
                $return = json_encode(codemsg(-1012));
                Log::login('【My/userModify】-[return]--'.json_encode($return));
                echo $return;die;
            }
            //更改手机验证码状态
            $dxwhere['phone'] = $mobile;
            $dxwhere['code'] = $code;
            $dxwhere['type'] = 4;
            $dxfind = model('DxSendCode')->findDxList($dxwhere);
            if ($dxfind == false){
                $return = json_encode(codemsg(-4007));
                Log::login('【My/userModify】-[return]--'.json_encode($return));
                echo $return;die;
            }
            $dxsvae['check'] = 1;
            $dxsvaewhere['mobile'] = $mobile;
            $dxsvaewhere['code'] = $code;
            $dxsvaewhere['source'] = 4;
            $savedx = M('dxsend_log')->where($dxsvaewhere)->update($dxsvae);
            if (empty($savedx)){
                $return = json_encode(codemsg(-7021));
                Log::login('【My/userModify】-[return]--'.json_encode($return));
                echo $return;die;
            }
            //更改手机号码
            $usersave['mobile'] = $mobile;
            $usersave['number'] = $mobile;
            $saveuser = M('users')->where($userwhere)->update($usersave);
            if (empty($saveuser) || $saveuser == false){
                $return = json_encode(codemsg(-7021));
                Log::login('【My/userModify】-[return]--'.json_encode($return));
                echo $return;die;
            }else{
                $return = json_encode(codemsg(5004));
                Log::login('【My/userModify】-[return]--'.json_encode($return));
                echo $return;die;
            }
        }
        $map = array();
        //存在生日
        if ($birthday){
            $checkbirthday = regcheck('time',$birthday);
            if ($checkbirthday){
                $return = $checkbirthday;
                Log::login('【My/userModify】-[return]--'.json_encode($return));
                echo $return;die;
            }else{
                $map['birthday'] = strtotime($birthday);
            }
        }
        //存在性别
        if ($sex==1 || $sex == 2 || $sex == 0){
            $map['sex'] = $sex;
        }
        //名字
        if ($nickname){
            $map['nickname'] = $nickname;
        }
        if ($email){
            $map['email'] = $email;
        }
        //签名
        if ($signature){
            $map['signature'] = $signature;
        }
        //更改信息
        $save = M('users')->where($userwhere)->update($map);
        if ($save){
            $return = json_encode(codemsg(5004));
            Log::login('【My/userModify】-[return]--'.json_encode($return));
            echo $return;die;
        }else{
            $return = json_encode(codemsg(-7021));
            Log::login('【My/userModify】-[return]--'.json_encode($return));
            echo $return;die;
        }
    }
    //修改头像
    public function handimg(){
        $userid = I('request.user_id');
//        $userid = 13;
        if (empty($userid) && $userid <=0){
            $return = codemsg(-9);
            Log::api('【My/handimg】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        Log::api('【My/handimg】-[参数]--'.json_encode($file.'--'.$userid));
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload' .DS. 'icon');
            if($info){
                $url ='/public' . DS . 'upload' .DS. 'icon'.DS.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                $return = codemsg(-7032);
                Log::api('【My/handimg】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }else{
            $return = codemsg(-7033);
            Log::api('【My/handimg】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $where['user_id'] = array('eq',$userid);
        $where['is_lock'] = array('eq',0);
        $map['head_pic'] = $url;
        $data = M('users')->where($where)->update($map);
        if (!$data){
            $return = json_encode(codemsg(-7021));
            Log::login('【My/handimg】-[return]--'.json_encode($return));
            echo $return;die;
        }else{
            $return = codemsg(5004);
            $return['data'] = $url;
            Log::login('【My/handimg】-[return]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }
    //留言
    public function feedback(){
        $file = request()->file('image');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $url = '';
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload' .DS. 'qr_code');
            if($info){
                $url ='\public' . DS . 'upload' .DS. 'qr_code'.DS.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                $return = codemsg(-7032);
                Log::api('【My/feedback】-[返回]--'.json_encode($return));
                echo json_encode($return);die;
            }
        }
        $userid = I('request.user_id');
        $con = I('request.con');
        if (empty($con) || empty($userid)){
            $return = codemsg(-9);
            Log::api('【My/feedback】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //存数据
        $map['message_img'] = $url;
        $map['user_id'] = $userid;
        $map['msg_content'] = $con;
        $map['msg_time'] = time();
        $data = M('feedback')->insert($map);
        if (!$data){
            $return = json_encode(codemsg(-7021));
            Log::login('【My/handimg】-[return]--'.json_encode($return));
            echo $return;die;
        }else{
            $return = json_encode(codemsg(5007));
            Log::login('【My/handimg】-[return]--'.json_encode($return));
            echo $return;die;
        }
    }
    //隐私设置
    public function privacy(){
        $userid = I('request.user_id');
        $privacy = I('request.privacyid');
        if (empty($privacy) || empty($userid)){
            $return = codemsg(-9);
            Log::api('【My/feedback】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $list = explode('-',$privacy);
        $map['is_email'] = $list[0];
        $map['is_mobile'] = $list[1];
        $map['is_auth'] = $list[2];
        $where['user_id'] = array('eq',$userid);
        $data = M('users')->where($where)->update($map);
        if (empty($data)){
            $return = json_encode(codemsg(-7021));
            Log::login('【My/handimg】-[return]--'.json_encode($return));
            echo $return;die;
        }else{
            $return = json_encode(codemsg(5004));
            Log::login('【My/handimg】-[return]--'.json_encode($return));
            echo $return;die;
        }
    }
}